package com.tliaswebmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tliaswebmanagement.mapper.EmpMapper;
import com.tliaswebmanagement.pojo.Emp;
import com.tliaswebmanagement.pojo.pageBean;
import com.tliaswebmanagement.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    /**
     * 根据id批量删除
     *
     * @param ids
     */

    @Override
    public void deleteIds(List<Integer> ids) {
        empMapper.deleteIds(ids);
    }

    /**
     * 分页查询 / 模糊查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Override
    public pageBean pageQuery(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//        List<Emp> rows = empMapper.pageQuery((page - 1) * pageSize, pageSize);
//        Long total = empMapper.count();
//        return new pageBean(total, rows);
        PageHelper.startPage(page, pageSize);
        Page<Emp> list = empMapper.pageQuery(name, gender, begin, end);
        return new pageBean(list.getTotal(), list.getResult());

    }

    /**
     * 添加员工
     *
     * @param emp
     */
    @Override
    public void add(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    /**
     * 根数据修改 根据id回显
     *
     * @param id
     * @return
     */
    @Override
    public Emp getId(Integer id) {
        Emp emp = empMapper.getById(id);
        return emp;
    }

    /**
     * 数据修改
     *
     * @param emp
     */
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.login(emp);
        return e;
    }
}
